home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3165 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: news.corp.hp.com!news
  2. From: Philip Walden <pwalden>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: Returning a reference
  5. Date: 22 Jan 1996 14:53:46 GMT
  6. Organization: PPO PGIS Design Automation
  7. Message-ID: <4e08dq$sqf@hpcc48.corp.hp.com>
  8. References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>
  9. NNTP-Posting-Host: walden.pa.itc.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.05 9000/715)
  14. X-URL: news:4cvsm2$5ig@dub-news-svc-4.compuserve.com
  15.  
  16. 100754.2730@compuserve.com (Martin Aupperle) wrote:
  17. >Borland C++ V4.5 does not allow to return a reference to a local
  18. >variable:
  19. >
  20. >int &doIt() {
  21. >
  22. >  int i = 7;
  23. >  return i;  // syntax error
  24. >  }
  25. >
  26. >I remember that I once had a compiler that did allow it (it gave me
  27. >only a warning). 
  28. >Which one is right? I think that it should be an error because after
  29. >the function has terminated, the reference has no data object it is
  30. >bound to any more.
  31.  
  32. It should be an error since the local variable goes out of scope when the
  33. function returns. You could make "i" static to rectify the problem.
  34. However, your function would no longer be re-entrant.
  35.  
  36. static int i = 7;
  37.  
  38. -- 
  39. --------------------------------------------------------------------------
  40. Philip Walden
  41. Hewlett Packard
  42. Product Generation Information Systems
  43. 1501 Page Mill Road, M/S 4U-6
  44. Palo Alto, CA 94304
  45. (415) 857-3899 FAX (415) 857-8234
  46. --------------------------------------------------------------------------
  47.  
  48.